package com.shxy.info.service;

import com.shxy.info.dao.IMovieDao;
import com.shxy.info.entity.Movie;
import com.shxy.info.util.MyFileUtils;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
@Log
public class MovieService implements IMovieService {
    @Autowired
    private IMovieDao movieDao;

    //支持的视频扩展名
    public static String[] movieExt = {"mp4"};

    @Override
    public List<Movie> importMoviesFromPath(String path) {
        List<File> files = MyFileUtils.getFilesFromPath(path, MovieService.movieExt);
        List<Movie> movies = new ArrayList<>();
        //检查文件列表是否为空
        if(files == null || files.size() == 0){
            return null;
        }

        //循环构造电影对象
        for (File f: files) {
            Movie movie = new Movie();
            String fname = f.getName();
            movie.setTitle(fname.substring(0, fname.lastIndexOf(".")));
            movie.setPath(f.getAbsolutePath());
            movie.setMd5(MyFileUtils.getMd5(f));
            movies.add(movie);
        }
        movies = movieDao.saveAll(movies);
        return movies;
    }
}
